# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Shortcut plugin is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    Shortcut plugin is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

VERSION = '0.2'

import gtk
import pygtk
import emesenelib.common
import Plugin
import UserList

class MainClass(Plugin.Plugin):
    '''Main plugin class'''

    def __init__(self, controller, msn):
        '''Contructor'''

        Plugin.Plugin.__init__(self, controller, msn )

        self.description = _('Send a Message to more contacts')
        self.authors = {'Orfeo Ciano':'orfeo18@gmail.com'}
        self.website = ''
        self.displayName = _('Multiple Send')
        self.name = 'MultipleSend'
        self.controller = controller
    
    def start(self):
        self.img = gtk.Image()
        self.img.set_from_file("/usr/share/icons/gnome/16x16/actions/mail_replyall.png")
        
        self.multipleSendButton = gtk.Button()
        self.multipleSendButton.set_image(self.img)
        self.multipleSendButton.set_relief(gtk.RELIEF_NONE)
        self.multipleSendButton.set_tooltip_text("Send a message to more than a contact")
        self.multipleSendButton.connect('clicked', self.showMultipleSendDialog)
        
        self.hbox = gtk.HBox()
        self.hbox.pack_start(self.multipleSendButton,True,False)
        
        self.controller.mainWindow.userPanel.vbox2.pack_start(self.hbox,True,False)
        self.hbox.show_all()
        
        self.enabled = True

    def stop(self):
        self.multipleSendButton.destroy()
        self.hbox.destroy()
        self.enabled = False
        
    def check(self):
        return (True, 'Ok')
        
    def showMultipleSendDialog(self, *args):
        '''this method is called when the user click the invite button'''
        online_contacts = self.controller.contacts.get_online_list()
        dictionary = {}
        
        for i in online_contacts:
            dictionary[i.account] = i
        
        members = {}
            
        MultipleSendWindow(self.controller, dictionary,self.controller.theme, None).run()
            
class MultipleSendWindow(gtk.Dialog):
    '''This class represent a list where the user can pick wich users 
    he want to send multiple messages'''

    def __init__(self, controller, onlineUsers, 
                    theme, callback):
        '''Constructor callback is called when a user is selected with 
        double click'''
        gtk.Dialog.__init__(self, _('Send Multiple Message'),None,
                     gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                     (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, \
                     gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))

        self.set_default_size(400, 300)
        self.callback = callback
        users = onlineUsers
        
        self.controller = controller

        self.userList = UserList.UserList(self.controller, theme,
            self.controller.config, False)

        self.selection = self.userList.get_selection()

        if gtk.gtk_version >= (2, 10, 0):
            self.userList.set_rubber_banding(True)
            self.userList.set_property("rubber-banding", True)
        
            self.selection.set_mode(gtk.SELECTION_MULTIPLE)
        
        group = emesenelib.ContactData.Group(_('Users'))

        for contact in controller.msn.contactManager.contacts.values():
            if contact and contact.status != 'FLN':               
                group.setUser(contact.email, contact)
            
        self.userList.fill({group.name : group})
        
        self.scroll = gtk.ScrolledWindow()
        self.scroll.set_policy(gtk.POLICY_AUTOMATIC , gtk.POLICY_AUTOMATIC)
        self.scroll.add_with_viewport(self.userList)
        
        self.connect('response', self.sendMessage)
        self.connect('delete-event', self.close)
        
        tipText1 = _("For multiple select hold CTRL key and click")
        self.tipLabel1 = gtk.Label()
        self.tipLabel1.set_markup("<small><i>" + tipText1 + "</i></small>")
        
        tipText2 = _("Insert here your message:")
        self.tipLabel2 = gtk.Label()
        self.tipLabel2.set_markup("<b>" + tipText2 + "</b>")
        
        self.messageInput = gtk.Entry()
        
        self.vbox.set_spacing(3)
        self.vbox.pack_start(self.scroll)
        self.vbox.pack_start(self.tipLabel1, False, False)
        self.vbox.pack_start(self.tipLabel2,False,False)
        self.vbox.pack_start(self.messageInput,False,False)
        self.vbox.show_all()
        
    def sendMessage(self, dialog, response_id):

        if response_id == gtk.RESPONSE_ACCEPT:
            model, iter = self.selection.get_selected_rows()

            for i in range(len(iter)):
                self.controller.conversationManager.newConversation(self.controller.msn,model[iter[i][0]][2],None,None)
                conversation = self.controller.conversationManager.getOpenConversation(model[iter[i][0]][2], None)
                message = self.messageInput.get_text()
                conversation[-1].sendMessage(message)
        self.close()
           
    def userListClicked(self, userList, t):

        if t and t[0] == 'user':
            self.callback(t[1].email)
            self.close()    

    def close(self, *args):
        '''close the window'''

        self.hide()
